var element;
var layer;
$(function(){
	layui.use(['layer','element'], function(){
		
	  	layer = layui.layer; 	// 弹层
	  	element = layui.element;// Tab的切换功能，切换事件监听等，需要依赖element模块
	  	
		leftMeunAdd();	// 左侧菜单添加
		getAllOrg();	// 加载所有组织
		
	})

})


/**
 * ------------------------------------------------------------------------------------------------------------------------------------------
 * 打开更多项目
 */
function openOrgSel(){
	layer.open({
		title : '选择切换项目',
		type:1,
		skin : 'layui-layer-molv',// 样式类名
		area: ['400px','400px'],
		btn : false,
		content:$('#openOrgSel'),
		time:0
	}, function() {
	});
	var s = '';
	for(var i=0; i<orgList.length; i++){
		var obj = orgList[i];
		if(obj.oid == lastOid){
			s += '<div class="org_box" style="background: #e9ecec;">';
		}else{
			s += '<div class="org_box">';
		}
		s += '	<div style="margin-left:10px;">';
		s += '		<div class="org_left"><span class="layui-badge layui-bg-blue" style="margin-right: 5px;">1</span>'+obj.oname+'</div>';
		if(obj.oid != lastOid){
			s += '		<button onclick="selectOrg('+obj.oid+')" class="layui-btn layui-btn-primary layui-btn-sm org_btn">切换</button>';
		}
		s += '	</div>';
		s += '</div>';
		if(obj.children && obj.children.length > 0){
			s += orgSel(obj.children,30,2);
		}
	}
	$("#openOrgSel").html(s);
}

/**
 * ------------------------------------------------------------------------------------------------------------------------------------------
 * 子级
 */
function orgSel(list, left, bl){
	var s = '';
	for(var i=0; i<list.length; i++){
		var obj = list[i];
		if(obj.oid == lastOid){
			s += '<div class="org_box" style="background: #e9ecec;">';
		}else{
			s += '<div class="org_box">';
		}
		s += '	<div style="margin-left:'+left+'px;">';
		s += '		<div class="org_left"><span class="layui-badge layui-bg-blue" style="margin-right: 5px;">'+bl+'</span>'+obj.oname+'</div>';
		if(obj.oid != lastOid){
			s += '		<button onclick="selectOrg('+obj.oid+')" class="layui-btn layui-btn-primary layui-btn-sm org_btn">切换</button>';
		}
		s += '	</div>';
		s += '</div>';
		if(obj.children && obj.children.length > 0){
			s += orgSel(obj.children,left+30,bl+1);
		}
	}
	return s;
}

var orgList ;
var orgCount = 0;
var orgName = "";

/**
 * ------------------------------------------------------------------------------------------------------------------------------------------
 * 获取所有组织-
 */
function getAllOrg(){
	$.post(ctxPath+"main/getAllOrg",{},function(res){
		var list = res.data;
		orgList = list;
		if(list){
			orgCount = 0;
			var s = '';
			var s1 = '';
			for(var i=0; i<list.length; i++){
				var obj = list[i];
				s += '<dl class="layui-nav-child">';
				if(obj.oid == lastOid){
					orgName = obj.oname;
					s += '<dd style="background: #ebecec;"><a href="#" onclick="selectOrg('+obj.oid+')">'+obj.oname+'</a></dd>';
				}else{
					s += '<dd><a href="#" onclick="selectOrg('+obj.oid+')">'+obj.oname+'</a></dd>';
				}
				if(obj.children && obj.children.length > 0){
					s += allOrgChid(obj.children,20);
					selectOrgName(obj.children);
				}
				if(orgCount > 3){
					s += '<hr><dd style="text-align: center;">';
					s += '  <a href="#" onclick="openOrgSel()" style="color:#336da0!important">更多小区>>></a>';
					s += '</dd>';
				}
				s += '</dl>';
			}
			$("#allOrg").html('<a href="javascript:;">'+orgName+'</a>'+s);
		}
	});
}

/**
 * ------------------------------------------------------------------------------------------------------------------------------------------
 * 子级
 */
function allOrgChid(list,left){
	var s = '';
	if(orgCount >= 3){
		return s;
	}
	for(var i=0; i<list.length; i++){
		orgCount = orgCount + 1;
		if(orgCount >= 3){
			return s;
		}
		var obj = list[i];
		if(obj.oid == lastOid){
			orgName = obj.oname;
			s += '<dd style="background: #ebecec;"><a href="#" onclick="selectOrg('+obj.oid+')">';
			s += '<span style="margin-left:'+left+'px;"></span>'+obj.oname+'</a></dd>';
		}else{
			s += '<dd><a href="#" onclick="selectOrg('+obj.oid+')">';
			s += '<span style="margin-left:'+left+'px;"></span>'+obj.oname+'</a></dd>';
		}
		if(obj.children && obj.children.length > 0){
			s += allOrgChid(obj.children,left+20);
		}
	}
	return s;
}

/**
 * ------------------------------------------------------------------------------------------------------------------------------------------
 * 获取项目名称
 * @param list
 */
function selectOrgName(list){
	for(var i=0; i<list.length; i++){
		var obj = list[i];
		if(obj.oid == lastOid){
			orgName = obj.oname;
		}
		if(obj.children && obj.children.length > 0){
			selectOrgName(obj.children);
		}
	}
}

/**
 * ------------------------------------------------------------------------------------------------------------------------------------------
 * 切换组织
 */
function selectOrg(value){
	$.post(ctxPath+"main/switchOrg",{"oid":value},function(res){
		if(res.success){
			window.location.reload();
		}
		layer.msg(res.msg);
	});
}

